package com.ms.content.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.R;
import com.ms.content.entity.BargainActivityEntity;
import com.ms.content.entity.BargainingItemEntity;
import com.ms.content.service.BargainingItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ms.content.entity.BargainingRecordEntity;
import com.ms.content.service.BargainingRecordService;


/**
 * 
 *
 * @author wc
 * @email sunlightcs@gmail.com
 * @date 2022-03-09 18:59:58
 */
@RestController
@RequestMapping("content/bargainingrecord")
public class BargainingRecordController {
    @Autowired
    private BargainingRecordService bargainingRecordService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private BargainingItemService bargainingItemService;


    /**
     * 用户通过fegin调用   进行帮砍
     */
    @RequestMapping("/kanjia")
    public R kanjia(@RequestParam("uuid") String uuid,@RequestParam("uid") Long uid){
      R r =   bargainingRecordService.userKanj(uuid,uid);
        return r;
    }

    /**
     * 获取链接
     */
    @RequestMapping("/getLink/{uid}")
    public R getLink(@PathVariable("uid") Long uid){
        String uuid = UUID.randomUUID().toString();
        BargainingItemEntity bargainingItemEntity =  bargainingItemService.se(uid);

        //将查询到的信息存入redis中
//        BargainingItemEntity byId = bargainingItemService.getById(id);
        System.out.println(bargainingItemEntity);
        stringRedisTemplate.opsForValue().set(uuid, JSON.toJSONString(bargainingItemEntity),1, TimeUnit.HOURS);
        return R.ok().put("uuid","file:///C:/Users/%E5%88%BA%E5%AE%A2/Desktop/vuvue/login.html?uuid="+uuid);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bargainingRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		BargainingRecordEntity bargainingRecord = bargainingRecordService.getById(id);

        return R.ok().put("bargainingRecord", bargainingRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BargainingRecordEntity bargainingRecord){
		bargainingRecordService.save(bargainingRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BargainingRecordEntity bargainingRecord){
		bargainingRecordService.updateById(bargainingRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		bargainingRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
